package com.wisezone.workflow.dao.ibatis;

import com.wisezone.common.Pagination;
import com.wisezone.common.dao.impl.BaseDaoImpl;
import com.wisezone.workflow.bean.search.WorkFlowSearch;
import com.wisezone.workflow.dao.WfProcessInstanceDao;
import com.wisezone.workflow.exception.WfDaoException;



/**
 * IBATIS 流程实例实现类
 * @author tss 2009-09-05
 * 
 */
public class WfProcessInstanceDaoImpl extends BaseDaoImpl implements WfProcessInstanceDao{

    public WfProcessInstanceDaoImpl() {
        super();
    }

    /**
	 * 当前流程实例相关信息查询
	 * @param hql
	 * @param searchEntity
	 * @return
	 * @throws Exception
	 */
	public<T> Pagination<T> listFormByPage(WorkFlowSearch searchEntity) throws WfDaoException{
		
		String actorGroupIds = searchEntity.getActorGroupIds();
		Long actorId = searchEntity.getActorId();
		StringBuffer sb = new StringBuffer();
		sb.append(" wf_process_instance t2,wf_activity_instance t3   where t2.id = t3.process_inst_id ");
		sb.append("and t1.process_inst_id = t2.id and  t3.id in  (select (activity_inst_id) from ");
		sb.append("wf_activity_inst_user where  ((user_id is not null and user_id = "+actorId+" ) or (user_id is ");
		sb.append("null  and user_group_id in ("+actorGroupIds+") ) ) ) and t3.id = (select max(activity_inst_id) from ");
		sb.append("wf_activity_inst_user where process_inst_id = t2.id  and  (user_id = "+actorId+" ");
		sb.append("or user_group_id in ("+actorGroupIds+") )  ) ");
	    
	    searchEntity.setHql(sb.toString());
	    
	    return super.listFormByPage(searchEntity);
		
	}

   

    

}